﻿#include "DocumentPrinter.h"

void DocumentPrinter::printExcel(){

    QString filePath = "E:\\Code\\Qt\\pkzi__boky__NCDIS2\\build-nuclide-Desktop_Qt_5_14_2_MinGW_64_bit-Debug\\report\\autoExport_实验20251113151731.xlsx";

    QAxObject* pApplication;
    QAxObject* pWorkBooks = NULL;
    QAxObject* pWorkBook = NULL;
    QAxObject* pSheets = NULL;
    QAxObject* pSheet = NULL;

    pApplication = new QAxObject();
    pApplication->setControl("Excel.Application");//连接Excel控件
    pApplication->dynamicCall("SetVisible(bool)", true);//false不显示窗体
    pApplication->setProperty("DisplayAlerts", false);//不显示任何警告信息。
    pApplication->setProperty("ScreenUpdating",false);//直接显示打印预览不需要显sheet内容
    pWorkBooks = pApplication->querySubObject("Workbooks");
    pWorkBook = pWorkBooks->querySubObject("Open(const QString &)", filePath);
    pSheets = pWorkBook->querySubObject("Sheets");//所有sheets
    pSheet = pSheets->querySubObject("Item(int)", 1);//第一个sheet

    //显示预览设置里的网格线（默认是没有网格线的）
    pSheet->querySubObject("PageSetup")->setProperty("PrintGridlines",true);

    //调出excel打印预览（此地方为阻塞状态，退出excel预览后，才执行下面的代码）
    pWorkBook->dynamicCall("PrintPreview");

    pWorkBook->dynamicCall("Close(bool)",false);//关闭工作簿
    //关闭excel
    if (pApplication != NULL)
    {
        pApplication->dynamicCall("Quit()");
        delete pApplication;
        pApplication = NULL;
    }

    //删除文件
    QFile tempFile(filePath);
    if(tempFile.exists())
    {
        tempFile.remove();
    }

}

